﻿using System;
using System.Collections.Generic;
using System.Peppers.UI;
using System.Peppers.Win32;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace System.Peppers.ShellExtensions
{
    /// <summary>
    /// Class for creating ContextMenuItems in a IContextMenu handler
    /// </summary>
    public sealed class ContextMenuItem : ContextMenuItemBase
    {
        #region Internal
        internal override MENUITEMINFO BuildStruct(ref uint id, ContextMenuBase shell)
        {
            MENUITEMINFO info = base.BuildStruct(ref id, shell);
            info.wID = ID = id++;
            info.dwTypeData = Text;
            info.fType = (uint)MF.STRING;
            info.fState = (uint)MF.ENABLED;
            info.fMask = (uint)(MIIM.ID | MIIM.TYPE | MIIM.STATE | MIIM.SUBMENU);
            if (Icon != IntPtr.Zero)
            {
                info.fMask |= (uint)MIIM.CHECKMARKS;
                info.hbmpChecked = Icon;
                info.hbmpUnchecked = Icon;
            }
            if (Children.Count > 0)
            {
                info.hSubMenu = User32.CreatePopupMenu();
                for (uint i = 0; i < Children.Count; i++)
                {
                    MENUITEMINFO child = Children[(int)i].BuildStruct(ref id, shell);
                    User32.InsertMenuItem(info.hSubMenu, i, true, ref child);
                }
            }
            //Add to shell
            shell.Commands[ID] = this;
            return info;
        }

        internal uint ID
        {
            get;
            set;
        }

        internal List<ContextMenuItemBase> Children
        {
            get;
            set;
        }

        internal void OnClick()
        {
            if (Click != null)
            {
                Click(this, EventArgs.Empty);
            }
        }
        #endregion

        private string _icon = string.Empty;

        /// <summary>
        /// Fires when item is clicked
        /// </summary>
        public event EventHandler Click;
        
        /// <summary>
        /// Constructor for ContextMenuItem
        /// </summary>
        public ContextMenuItem()
        {
            Children = new List<ContextMenuItemBase>();
        }

        /// <summary>
        /// Display text, "&amp;" puts an underline like in WinForms
        /// </summary>
        public string Text
        {
            get;
            set;
        }

        /// <summary>
        /// Adds items to a submenu
        /// </summary>
        /// <param name="item">Child item</param>
        public void AddChild(ContextMenuItemBase item)
        {
            Children.Add(item);
        }

        /// <summary>
        /// Number of children
        /// </summary>
        public int ChildrenCount
        {
            get { return Children.Count; }
        }

        /// <summary>
        /// A 13x13 image used as the icon, transparency does not work
        /// </summary>
        public Bitmap Icon
        {
            get;
            set;
        }
    }
}
